﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Entry;

namespace COFE.Core.Logger
{
    public class EventLog : PropertyHostBase<IEventLog>, IEventLog
    {

        #region Constructor

        public EventLog(Int64 id, DateTime time, string sender,
            string userId, EventArgs eventArgs)
            : base(
            new EventLogPropertyProvider(id, time, sender, userId, eventArgs)
            )
        {
            _thisObj = this;
        }

        #endregion

        #region Methods

        public IEventLog<T> ToGeneric<T>()
            where T : EventArgs
        {
            if (!(this.EventArgs is T))
                throw new ArgumentException();
            if (this.EventArgs is IEventLog<T>)
                return (IEventLog<T>)this;
            return new EventLog<T>(Id, UtcTime, Sender, UserId, (T)EventArgs);
        }
        #endregion

        #region Data

        #endregion

        #region Public Properties

        public Int64 Id
        {
            get
            {
                return
                    PrimaryPropertyProvider
                    .GetProperty(EventLogProperties.Id).ValueAsInt64;
            }
        }

        public DateTime UtcTime
        {
            get
            {
                return
                    PrimaryPropertyProvider
                    .GetProperty(EventLogProperties.Time).ValueAsUtcDateTime;
            }
        }
        public string Sender
        {
            get
            {
                return
                    PrimaryPropertyProvider
                    .GetProperty(EventLogProperties.Sender).ValueAsString;
            }
        }
        public string UserId
        {
            get
            {
                return
                    PrimaryPropertyProvider
                    .GetProperty(EventLogProperties.UserId).ValueAsString;
            }
        }
        public EventArgs EventArgs
        {
            get
            {
                return
                    PrimaryPropertyProvider
                    .GetProperty(EventLogProperties.Event).Value as EventArgs;
            }
        }

        #endregion

    }


    public class EventLog<T> : EventLog, IEventLog<T>
        where T : EventArgs
    {
        public new T EventArgs { get { return (T)base.EventArgs; } }
        
        public EventLog(Int64 id, DateTime time, string sender, string userId, T eventArgs)
            : base(id, time, sender, userId, eventArgs)
        {
        }
    }
}
